﻿using System;

namespace SpoolFileLibrary.Reader.Winspool {
    public class DeviceMode : IDeviceMode {
        private readonly PaperSource defaultSource;
        private readonly string deviceName;
        private readonly DEVMODE devmode;
        private readonly DitherType ditherType;
        private readonly string formName;
        private readonly ImageColorManagementIndent imageColorManagementIndent;
        private readonly ImageColorManagementMethod imageColorManagementMethod;
        private readonly MediaType mediaType;
        private readonly PaperSize paperSize;
        private readonly PrintQuality printQuality;

        [CLSCompliant(false)]
        public DeviceMode(DEVMODE devmode) {
            this.devmode = devmode;
            unsafe {
                this.deviceName = new string(devmode.dmDeviceName);
                this.formName = new string(devmode.dmFormName);
            }
            this.paperSize = new PaperSize(devmode.dmPaperSize);
            this.defaultSource = new PaperSource(devmode.dmDefaultSource);
            this.printQuality = new PrintQuality(devmode.dmPrintQuality);
            this.imageColorManagementMethod = new ImageColorManagementMethod((int) devmode.dmICMMethod);
            this.imageColorManagementIndent = new ImageColorManagementIndent((int) devmode.dmICMIntent);
            this.mediaType = new MediaType((int) devmode.dmMediaType);
            this.ditherType = new DitherType((int) devmode.dmDitherType);
        }

        #region IDeviceMode Members

        public string DeviceName {
            get { return this.deviceName; }
        }

        public short SpecVersion {
            get { return (short) this.devmode.dmSpecVersion; }
        }

        public short DriverVersion {
            get { return (short) this.devmode.dmDriverVersion; }
        }

        public short Size {
            get { return (short) this.devmode.dmSize; }
        }

        public short DriverExtra {
            get { return this.devmode.dmDriverExtra; }
        }

        public Fields Fields {
            get { return (Fields) this.devmode.dmFields; }
        }

        public Orientation Orientation {
            get { return (Orientation) this.devmode.dmOrientation; }
        }

        public PaperSize PaperSize {
            get { return this.paperSize; }
        }

        public short PaperLength {
            get { return this.devmode.dmPaperLength; }
        }

        public short PaperWidth {
            get { return this.devmode.dmPaperWidth; }
        }

        public short Scale {
            get { return this.devmode.dmScale; }
        }

        public short Copies {
            get { return this.devmode.dmCopies; }
        }

        public PaperSource DefaultSource {
            get { return this.defaultSource; }
        }

        public PrintQuality PrintQuality {
            get { return this.printQuality; }
        }

        public bool Color {
            get { return this.devmode.dmColor == 1 ? false : true; }
        }

        public Duplex Duplex {
            get { return (Duplex) this.devmode.dmDuplex; }
        }

        public short YResolution {
            get { return this.devmode.dmYResolution; }
        }

        public TrueTypeOption TTOption {
            get { return (TrueTypeOption) this.devmode.dmTTOption; }
        }

        public bool Collate {
            get { return this.devmode.dmCollate == 0 ? false : true; }
        }

        public string FormName {
            get { return this.formName; }
        }

        public NupPrinting Nup {
            get { return (NupPrinting) this.devmode.dmNup; }
        }

        public ImageColorManagementMethod ImageColorManagementMethod {
            get { return this.imageColorManagementMethod; }
        }

        public ImageColorManagementIndent ImageColorManagementIndent {
            get { return this.imageColorManagementIndent; }
        }

        public MediaType MediaType {
            get { return this.mediaType; }
        }

        public DitherType DitherType {
            get { return this.ditherType; }
        }

        #endregion
    }
}